<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="basaId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >
          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openManagerStateDialog(record)">运行数据</a>
                <a-divider type="vertical" />
                <a @click="openCommandDialog(record)">命令下发</a>
              </a-space>
            </template>

            <template v-if="column.key === 'malfunctionStatus'">
              <a-tag v-if="record.basaRealtimeData == null || record.basaRealtimeData.malfunctionStatus === null" color="grey">无数据</a-tag>
              <a-tag v-else-if="record.basaRealtimeData.malfunctionStatus === 0" color="red">正常</a-tag>
              <a-tag v-else color="red">异常</a-tag>
            </template>

            <template v-if="column.key === 'warnningStatus'">
              <a-tag v-if="record.basaRealtimeData == null || record.basaRealtimeData.warnningStatus === null" color="grey">无数据</a-tag>
              <a-tag v-else-if="record.basaRealtimeData.warnningStatus === 0" color="red">正常</a-tag>
              <a-tag v-else color="red">异常</a-tag>
            </template>

            <template v-if="column.key === 'statuscolumn'">
              <a-tag v-if="record.deviceRecord != null && record.deviceRecord.recordStatus == 1" color="green">正常</a-tag>
              <a-tag v-else color="red">异常</a-tag>
            </template>

          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <device-managerdevice-basa-realtimedata-state-modal  v-if="showManagerState" v-model:visible="showManagerState" :data="current" @done="reload" />
    <DeviceManagerdeviceBasaCommandCmdModel v-if="showCommandModel" v-model:visible="showCommandModel" :data="current" @done="reload" />
  </div>
</template>

<script>
import { BasaApi } from '@/api/property/basa/BasaApi';
import DeviceManagerdeviceBasaRealtimedataStateModal from './realtimedata/state-modal.vue'
import DeviceManagerdeviceBasaCommandCmdModel from './command/cmd-modal.vue'
import { message } from 'ant-design-vue';
export default {
  name: 'DeviceManagerdeviceBasaRealtimeList',
  components: {
    DeviceManagerdeviceBasaRealtimedataStateModal,
    DeviceManagerdeviceBasaCommandCmdModel
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: 'ID',
          dataIndex: 'basaId'
        },
        {
          title: '巴杀名称',
          dataIndex: 'basaName'
        },
        {
          title: '设备状态',
          dataIndex: 'deviceRecord',
          key : 'statuscolumn',
        },
        {
          title: '水温度(℃)',
          dataIndex: 'basaRealtimeData',
          customRender: function(row) {
            if (row.record.basaRealtimeData != null && row.record.basaRealtimeData.waterTemperature !== null) {
              return row.record.basaRealtimeData.waterTemperature;
            } else {
              return '无数据';
            }
          }
        },
        {
          title: '剩余执行分钟(min)',
          dataIndex: 'basaRealtimeData',
          customRender: function(row) {
            if (row.record.basaRealtimeData != null && row.record.basaRealtimeData.remainTime !== null) {
              return row.record.basaRealtimeData.remainTime;
            } else {
              return '无数据';
            }
          }
        },
        {
          title: '工作状态',
          dataIndex: 'basaRealtimeData',
          customRender: function(row) {
            if (row.record.basaRealtimeData != null && row.record.basaRealtimeData.workStatus !== null) {
              if (row.record.basaRealtimeData.workStatus == 0) {
                return "none";
              } else if (row.record.basaRealtimeData.workStatus == 1) {
                return "profile1";
              } else if (row.record.basaRealtimeData.workStatus == 2) {
                return "profile2";
              } else if (row.record.basaRealtimeData.workStatus == 163) {
                return "profile3,ramp";
              } else if (row.record.basaRealtimeData.workStatus == 179) {
                return "profile3,Frozen";
              } else if (row.record.basaRealtimeData.workStatus == 195) {
                return "profile3,Refrigerator";
              } else if (row.record.basaRealtimeData.workStatus == 211) {
                return "profile3,room temperature";
              } else if (row.record.basaRealtimeData.workStatus == 211) {
                return "profile4";
              }
            } else {
              return '无数据';
            }
          }
        },
        {
          title: '故障状态',
          dataIndex: 'basaRealtimeData',
          key : 'malfunctionStatus',
        },
        {
          title: '告警状态',
          dataIndex: 'basaRealtimeData',
          key : 'warnningStatus',
        },
        {
          title: '更新时间',
          dataIndex: 'basaRealtimeData',
          customRender: function(row) {
            if (row.record.basaRealtimeData != null && row.record.basaRealtimeData.updateTime !== null) {
              return row.record.basaRealtimeData.updateTime;
            } else {
              return '无数据';
            }
          }
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showManagerState : false,
      showCommandModel : false
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2024/03/10 22:34
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2024/03/10 22:34
     */
    reset() {
      this.where = {};
      this.$nextTick(() => {
        this.reload();
      });
    },


    datasource({ page, limit, where, orders }) {
      return BasaApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    },

    openManagerStateDialog(row) {
      this.current = row;
      this.showManagerState = true;
    },
    openCommandDialog(row) {
      if (row.recordId != null && row.recordId > 0) {
        this.current = row;
        this.showCommandModel = true;
      } else {
        message.error('请先绑定4G模块');
      }

    }
  }
};
</script>
